package com.lc.afbx.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.lc.afbx.BaseApplication;
import com.lc.afbx.R;
import com.lc.afbx.adapters.TeamAdapter;
import com.lc.afbx.conn.GetApiTeam;
import com.lc.afbx.conn.GetApiTeamInfomation;
import com.lc.afbx.conn.GetApiTeamSearch;
import com.lc.afbx.util.MyToast;
import com.lc.afbx.util.PrefrenceConfig;
import com.zcx.helper.app.AppCallBack;
import com.zcx.helper.bound.BoundView;
import com.zcx.helper.http.AsyCallBack;
import com.zcx.helper.util.UtilLog;
import com.zcx.helper.util.UtilToast;

import cn.trinea.android.common.util.ToastUtils;

/**
 * Created by Administrator on 2016/5/20.
 */
public class TeamActivity extends BaseActivity implements View.OnClickListener {

    @BoundView(R.id.commissionRule) private TextView commissionRule;
    private View header;
    private ListView team_lv;

    private GetApiTeam.Data data;
    private TeamAdapter adapter;
    private TextView belong;
    private TextView leixing;
    private TextView name;
    private LinearLayout title;
    private String preName;
    private String otherInfoId;


    public GetApiTeam getApiTeam = new GetApiTeam(new AsyCallBack<GetApiTeam.Info>() {

        @Override
        public void onSuccess(String toast, int type, GetApiTeam.Info info) throws Exception {

            data = info.data;
            belong.setText(data.team);
            leixing.setText(data.level);
            name.setText(preName + data.nickname);

            getApiTeamSearch.tid = info.data.team_id; //请求搜索结果的团队id的参数  在这设置


        }

        @Override
        public void onFail(String toast, int type) throws Exception {

            UtilToast.show(context, toast);

        }
    });

    //团队成员 查询接口
    private GetApiTeamSearch getApiTeamSearch = new GetApiTeamSearch(new AsyCallBack<GetApiTeamSearch.Info>() {
        @Override
        public void onSuccess(String toast, int type, GetApiTeamSearch.Info info) throws Exception {

            title.setVisibility(View.VISIBLE);

            String preName = name.getText().toString();

            team_lv.setAdapter(adapter = new TeamAdapter(TeamActivity.this, info.dataList, preName));


        }

        @Override
        public void onFail(String toast, int type) throws Exception {

            MyToast.showToast("没有更多数据了", context);
        }
    });


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_team);

        header = getLayoutInflater().inflate(R.layout.header_team, null);
        BaseApplication.ScaleScreenHelper.loadView((ViewGroup) header);

        team_lv = (ListView) findViewById(R.id.team_lv);
        team_lv.addHeaderView(header, null, false);
        team_lv.setDivider(null);
        team_lv.setAdapter(null);  //TODO 这设置adapter了

        belong = ((TextView) team_lv.findViewById(R.id.belong_tuandui));
        leixing = ((TextView) team_lv.findViewById(R.id.type_tuandui));
        name = ((TextView) team_lv.findViewById(R.id.name_tuandui));

        //这个listview上部的东西 在 header部分里  默认是隐藏的
        title = ((LinearLayout) team_lv.findViewById(R.id.title_team));

        Bundle bundle = getIntent().getExtras();

        try {

            preName = bundle.getString("preName", "");
            otherInfoId = bundle.getString("otherInfoId", BaseApplication.BasePreferences.readUserId());

        } catch (Exception e) {
            e.printStackTrace();
        }

        getApiTeam.userid = otherInfoId;
        getApiTeam.execute(this);


        commissionRule.setOnClickListener(this);

    }


    @Override
    public void onClick(View view) {


        switch (view.getId()) {

            case R.id.back:
                finish();
                return;

            case R.id.team_chooseTeam_rl:

                startActivityForResult(new Intent(this, TeamChooseActivity.class).putExtra(PrefrenceConfig.INTENT_TAG, otherInfoId), 1);

                break;

            case R.id.commissionRule:

                startActivity(new Intent(this, CommissionRuleActivity.class));
                break;

            case R.id.search_member:

                //请求搜索结果的团队id的参数 在上边设置了，回调方法执行 就会被重新设置
                getApiTeamSearch.userid = getApiTeam.userid;
                getApiTeamSearch.execute(context);
                return;

        }


    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {

        if (data != null) {

            Bundle bundle = data.getExtras();

            getApiTeamSearch.tid = bundle.getString("id");
            belong.setText(bundle.getString("title"));

        }

    }


}
